package com.ruiyian.web.controller.system;

import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.common.utils.SecurityUtils;
import com.ruiyian.system.domain.Document;
import com.ruiyian.system.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2022/6/20
 * Desc: 文件档案
 */
@RestController
@RequestMapping("/system/doc")
public class DocumentController extends BaseController {

    @Autowired
    private DocumentService documentService;

    /**
     * 分页查询
     * @param document
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(Document document)
    {
        startPage();
        List<Document> list = documentService.list(document);
        return getDataTable(list);
    }

    /**
     * 修改名称
     * @param document
     * @return
     */
    @PostMapping("/updateName")
    public AjaxResult updateName(@RequestBody Document document){
        document.setUpdateBy(SecurityUtils.getUsername());
        documentService.updateName(document);
        return AjaxResult.success();
    }

    /**
     * 添加
     * @param document
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Document document){
        document.setCreateBy(SecurityUtils.getUsername());
        document.setSuffix(document.getUrl().substring(document.getUrl().lastIndexOf(".") + 1));
        documentService.save(document);
        return AjaxResult.success();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult deal(@RequestBody Long id){
        documentService.delete(id);
        return AjaxResult.success();
    }

}
